package com.hou.netty.tcp01.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;

//客户端处理数据
public class ClientHandler extends SimpleChannelInboundHandler<String> {
    //处理服务端返回的数据
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String str) throws Exception {
        try {
            System.out.println("客户端接受到服务端返回的数据:" + str);
        } finally {
            //如果没有进行写操作,就一定要释放
            ReferenceCountUtil.release(str);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();//打印信息
        ctx.close();//关闭通道
    }
}
